package cn.itcast.dao.impl;

import cn.itcast.dao.DaoException;
import cn.itcast.dao.UserDao;
import cn.itcast.po.User;
import utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoJdbcImpl implements UserDao {
    @Override
    public void addUser(User user) {
        // 初始化
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "insert into user(username, birthday, sex, address) values(?,?,?,?)";

        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement(sql);

            ps.setString(1, user.getName());
            ps.setDate(2, new java.sql.Date(user.getDate().getTime()));
            ps.setString(3, user.getSex());
            ps.setString(4, user.getAddress());

            ps.executeUpdate();
        } catch (SQLException e) {
            throw new DaoException(e);
        }finally {
            JdbcUtils.free(ps, conn, null);
        }

    }

    @Override
    public void deleteUser(User user) {
        // 初始化
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "DELETE FROM user WHERE id=?";

        try {
            conn = JdbcUtils.getConnection();

            ps = conn.prepareStatement(sql);
            ps.setInt(1, user.getId());
            ps.executeUpdate();

        } catch (SQLException e) {
            throw new DaoException(e);
        }finally {
            JdbcUtils.free(ps, conn, null);
        }

    }

    @Override
    public void updateUser(User user) {
        // 初始化
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "UPDATE user SET address=? WHERE id=?";

        try {
            conn = JdbcUtils.getConnection();

            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getAddress());
            ps.setInt(2, user.getId());
            ps.executeUpdate();

        } catch (SQLException e) {
            throw new DaoException(e);
        }finally {
            JdbcUtils.free(ps, conn, null);
        }
    }

    @Override
    public User findUser(Integer id) {
        // 初始化
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM user WHERE id=?";

        try {
            conn = JdbcUtils.getConnection();

            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);

            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(id);
                user.setName(rs.getString("username"));
                user.setDate(rs.getDate("birthday"));
                user.setSex(rs.getString("sex"));
                user.setAddress(rs.getString("address"));
                return user;
            }

        } catch (SQLException e) {
            throw new DaoException(e);
        }finally {
            JdbcUtils.free(ps, conn, null);
        }
        return null;
    }
}
